package com.easymin.weixin.controller.v2;

import com.google.common.base.Preconditions;
import com.easymin.weixin.common.transport.Error;
import com.easymin.weixin.common.transport.Result;
import com.easymin.weixin.entity.OrderType;
import com.easymin.weixin.manager.ErrandManager;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/errand/v2/orderType")
public class OrderTypeRes {
  
  private static final Logger logger = LoggerFactory.getLogger(OrderTypeRes.class);

  @Autowired
  private ErrandManager errandManager;
  
  /**
   * 查询跑腿类型.
   */
  @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findByParam(
      @RequestParam(required = false, value = "companyId") Long companyId,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {
      List<OrderType> list = new ArrayList<OrderType>();

      list = errandManager.findByCompanyId(companyId, appKey);

      Result<List<OrderType>> result = new Result<List<OrderType>>(0, "成功", list);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {
      logger.error(ex.getMessage());
      Error error = new Error(-4, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

}
